package com.wangbo.box.state;

/**
 * 状态模式
 *
 * 对有状态的对象，把复杂的“判断逻辑”提取到不同的状态对象中，允许状态对象在其内部状态发生改变时改变其行为。
 */
public class StatePattern {


    static class Context{
        private State state;

        public void setState(State state) {
            this.state = state;
        }

        public Context() {
            state = new StateA();
        }

        public void handle(){
            state.handle(this);
        }
    }
    static abstract class State{
        abstract void handle(Context context);
    }

    static class StateA extends State{

        @Override
        void handle(Context context) {
            System.out.println("state A");
            context.setState(new StateB());
        }
    }

    static class StateB extends State{

        @Override
        void handle(Context context) {
            System.out.println("state B");
            context.setState(new StateA());
        }
    }

    public static void main(String[] args) {
        Context context = new Context();
        context.handle();
        context.handle();
        context.handle();
    }
}